<template>
  <a-select
    v-bind="$attrs"
    v-model="checked"
    @change="handleChange"
    @popupScroll="handlePopupScroll"
  >
    <a-select-option
      v-for="item in options"
      :key="item[optionValue]"
      :value="item[optionValue]"
      >
      <slot name="prefix" :item="item"></slot>
      <span>{{ item[optionTitle] }}</span>
      <slot name="suffix" :item="item"></slot>
    </a-select-option>
    <div slot="dropdownRender" slot-scope="menu">
      <v-nodes :vnodes="menu" />
      <slot name="footer"></slot>
    </div>
  </a-select>
</template>

<script>
import { cloneDeep } from 'lodash-es'

export default {
  components: {
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes,
    },
  },
  props: {
    modelValue: {
      type: [String, Number, Array],
      default: undefined
    },
    options: {
      type: Array,
      default: () => []
    },
    optionValue: {
      type: [String, Number],
      default: 'value'
    },
    optionTitle: {
      type: String,
      default: 'title'
    }
  },
  data() {
    return {
      checked: undefined,
    }
  },
  watch: {
    modelValue: {
      handler(nval) {
        this.checked = cloneDeep(nval);
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleChange(value) {
      this.$emit('update:modelValue', value);
    },
    handlePopupScroll(e) {}
  }
}
</script>

<style lang="less" scoped></style>
